package com.wing.order.controller.app;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.request.UseInventoryInoutForm;
import com.wing.common.constant.CabAuthCode;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.order.model.request.OldNewOrderForm;
import com.wing.order.model.response.OldNewOrderVO;
import com.wing.order.service.OldNewOrderService;
import com.wing.web.utils.JwtUtils;
import com.wing.web.utils.LocaleMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-以旧换新(存旧刀，领新刀)管理", tags = "以旧换新(存旧刀，领新刀)管理")
@RestController("appOldNewOrderController")
@RequestMapping("order/app/oldNewOrder")
@Slf4j
public class OldNewOrderController {

    @Autowired
    private OldNewOrderService oldNewOrderService;
    @Autowired
    private LocaleMessage localeMessage;

    @GetMapping()
    public PageResult<OldNewOrderVO> list(
            @RequestParam(name = "page", value = "第几页", defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit", value = "每页多少条", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort", value="排序字段名", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "desc", required = false) String order
    ) {
        Page<OldNewOrderVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        Page<OldNewOrderVO> list = oldNewOrderService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "存暂存刀具", notes = "")
    @PostMapping("/inventoryInOut")
    public JsonResult inventoryInOut(@RequestBody UseInventoryInoutForm useInventoryInoutForm) {
        try {
            log.info("inventoryInOut param:{}", JSON.toJSONString(useInventoryInoutForm));
            String cabAuthCodes= JwtUtils.getCabAuthCodes();
            if (
//                    CabinetCargoSpace.SpaceNature.personal.equals(useInventoryInoutForm.getStorageType())&&
                            !cabAuthCodes.contains(CabAuthCode.OLD_NEW_AUTH_CODE)){
                return JsonResult.fail("此用户无换新权限,请配置此用户角色的换新权限");
            }
            useInventoryInoutForm.setUuid(UUID.randomUUID().toString());
            return oldNewOrderService.inventoryInOut(useInventoryInoutForm);
        } catch (Exception e) {
            log.error("inventoryInOut fail", e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @GetMapping("/detail/{id}")
    public JsonResult detail(@PathVariable("id") Long oldNewOrderId) {
        OldNewOrderVO oldNewOrderVO = oldNewOrderService.get4appById(oldNewOrderId);
        if (oldNewOrderVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), oldNewOrderVO);
    }

    @PostMapping()
    public JsonResult add(@Validated OldNewOrderForm oldNewOrderForm) {
        if (oldNewOrderService.add(oldNewOrderForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

}
